# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
### Reducing Prejudice and Support for Religious 
### Nationalism Through Conversations on WhatsApp 

### Author: Rajeshwari Majumdar
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# File:     setup_classify.R
# Purpose:  show steps to produce "prejudice_binary" classification   
#           note: this variable already exists in the datasets; the
#           script simply reconstructs it following Appendix C.3
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# Load data
rm(list=ls())
load("data/Completes_Merged_Anonymized.RData")

# Remove the variables to be reconstructed
d = d %>% select(-avg.ingr.outgr, -prejudice_binary)

# Determine attribute ratings for each person's ingroup and outgroup 
d = d %>%
  mutate(
    # Patriotic
    patriot.ingr = case_when(
      religion == "Hindu"  ~ gr_patriot_hindu,
      religion == "Muslim" ~ gr_patriot_muslim,
      TRUE ~ NA_real_
    ),
    patriot.outgr = case_when(
      religion == "Hindu"  ~ gr_patriot_muslim,
      religion == "Muslim" ~ gr_patriot_hindu,
      TRUE ~ NA_real_
    ),
    # Peaceful 
    peace.ingr = case_when(
      religion == "Hindu"  ~ gr_peace_hindu,
      religion == "Muslim" ~ gr_peace_muslim,
      TRUE ~ NA_real_
    ),
    peace.outgr = case_when(
      religion == "Hindu"  ~ gr_peace_muslim,
      religion == "Muslim" ~ gr_peace_hindu,
      TRUE ~ NA_real_
    ),
    # Hardworking
    hardwork.ingr = case_when(
      religion == "Hindu"  ~ gr_hardwork_hindu,
      religion == "Muslim" ~ gr_hardwork_muslim,
      TRUE ~ NA_real_
    ),
    hardwork.outgr = case_when(
      religion == "Hindu"  ~ gr_hardwork_muslim,
      religion == "Muslim" ~ gr_hardwork_hindu,
      TRUE ~ NA_real_
    ),
    # Honest
    honest.ingr = case_when(
      religion == "Hindu"  ~ gr_honest_hindu,
      religion == "Muslim" ~ gr_honest_muslim,
      TRUE ~ NA_real_
    ),
    honest.outgr = case_when(
      religion == "Hindu"  ~ gr_honest_muslim,
      religion == "Muslim" ~ gr_honest_hindu,
      TRUE ~ NA_real_
    ),
    
    # Difference variables
    patriot.ingr.outgr = patriot.ingr - patriot.outgr,
    peace.ingr.outgr = peace.ingr - peace.outgr,
    hardwork.ingr.outgr = hardwork.ingr - hardwork.outgr,
    honest.ingr.outgr = honest.ingr - honest.outgr
    
  )


# Calculate (average) distance between ingroup and outgroup ratings 
ingr_vars  = c("patriot.ingr", "peace.ingr", "hardwork.ingr", "honest.ingr")
outgr_vars = c("patriot.outgr", "peace.outgr", "hardwork.outgr", "honest.outgr")

d = d %>%
  mutate(
    total.ingr  = rowSums(across(all_of(ingr_vars)),  na.rm = TRUE),
    total.outgr = rowSums(across(all_of(outgr_vars)), na.rm = TRUE)
  )

d$total.ingr.outgr = d$total.ingr - d$total.outgr
d$avg.ingr.outgr = d$total.ingr.outgr / 4


# Produce prejudice classification
d$prejudice_binary = ifelse(d$avg.ingr.outgr > 0, 1, 0)
